﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ConcordWinManager
{
    public partial class RezidentForm : Form
    {
        public Rezident rezident;

        private Form parentForm;

        public RezidentForm(Form parentForm)
        {
            InitializeComponent();
            rezident = new Rezident();

            this.parentForm = parentForm;
        }

        private void btnChangeItem_Click(object sender, EventArgs e)
        {

            Tools.RunWithProgressIndicator(this, delegate
            {
                if (dataGridViewTable.SelectedRows.Count > 0)
                {
                    this.rezident.SelectById(
                        (int)dataGridViewTable.Rows[dataGridViewTable.SelectedCells[0].RowIndex].Cells["id_rezident"].Value);
                }
            });
            
            this.Close();
        }

        private void RezidentForm_Load(object sender, EventArgs e)
        {
            btnAdd.Visible = Settings.consultant.user.IsAdminRole;
            btnChange.Visible = Settings.consultant.user.IsAdminRole;

            UpdateForm();
        }

        public void UpdateForm()
        {
            DataSet ds = new DataSet();
            Tools.RunWithProgressIndicator(this, delegate { rezident.SelectAll(ref ds); });

            dataGridViewTable.DataSource = ds.Tables[0];
            dataGridViewTable.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGridViewTable.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            dataGridViewTable.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            dataGridViewTable.Columns["Name"].HeaderText = "Название";
            dataGridViewTable.Columns["Name2"].HeaderText = "Название для печати";
            dataGridViewTable.Columns["Note"].HeaderText = "Примечение";
            dataGridViewTable.Columns["id_rezident"].Visible = false;
            dataGridViewTable.Columns["Details"].Visible = false;

            //dataGridViewTable.Columns["id_address"].Visible = false;

            dataGridViewTable.Refresh();
        }

        private void btnChange_Click(object sender, EventArgs e)
        {
            RezidentEditForm rezidentEditForm = new RezidentEditForm(this, "Изменение",
                (int)dataGridViewTable.Rows[dataGridViewTable.SelectedCells[0].RowIndex].Cells["id_rezident"].Value);
            rezidentEditForm.ShowDialog();
            UpdateForm();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            RezidentEditForm rezidentEditForm = new RezidentEditForm(this, "Добавление");
            rezidentEditForm.ShowDialog();
            UpdateForm();
        }
    }
}
